<?php
namespace Kuoqi\Dom{
class DomNodelist{
	
	public $length = 0;//拥有多少个元素
	
	public $items = array();

    /**
     * @param $index
     * @return DomElement|mixed
     * User kuoqi
     * Datetime 2022/3/2 22:12
     * Remark 获取一个item
     */
	public function getItem($index){
	    return $this->item($index);
    }

	//item
	public function item($index){
		$element =  isset($this->items[$index])?$this->items[$index]:(new DomElement('div'));
		$element = new DomElement($element);
		return $element;
	}
	//获取长度
	public function getLength(){
		return count($this->items);	
	}
	//push到这里
	public function push($dom){
		$this->items[] = $dom;
	}
	//获取子节点信息
	public function innerHtml(){
		$element = $this->item(0);
		$innerHTML= ''; 
		$children = $element->actElement->childNodes; 
		foreach ($children as $child) { 
		 	$innerHTML .= $child->ownerDocument->saveXML( $child ); 
		} 
		return $innerHTML; 
	}
	//html
	public function html(){
		$innerHTML= ''; 
		foreach($this->items as $k=>$v){
			$element = new DomElement($this->items[$k]);
			$children = $element->actElement->childNodes; 
			foreach ($children as $child) { 
				$innerHTML .= $child->ownerDocument->saveXML( $child ); 
			} 	
		}
		return $innerHTML; 	
	}
	//获取文本内容
	public function text(){
		return $this->item(0)->text();	
	}
	//遍历
	public function each($callback){
		foreach($this->items as $k=>$v){
			$callback(new DomElement($this->items[$k]),$k);	
		}	
	}
	public function each2($callback){
		foreach($this->items as $k=>$v){
			$callback($this->items[$k],$k);	
		}	
	}
	//获取属性
	public function getAttr($attrName){
		return $this->item(0)->getAttribute($attrName);	
	}
	//设置属性
	public function setAttr($attrName,$attrVal){
		return $this->item(0)->setAttribute($attrName,$attrVal);		
	}
	//删除某个属性
	public function removeAttr($attrName){
		return $this->item(0)->removeAttribute($attrName);
	}
	//直接删除本来的节点
	public function remove(){
		$this->each(function($ele,$k){
			$ele->remove();
		});
	}
	//查找
	public function find($selector){
		return self::querySelector($selector);	
	}
	//获取选择器列表
	public function querySelector($selector){
		$selector = DomSelector::getSelector($selector);
		$nodelist = new DomNodelist();
		$length = 0;
		foreach($this->items as $k=>$v){
			$nodes = $v->getElementsByTagName($selector['tagname']);
			foreach($nodes as $vv){
				$hasit = 1;
				//判定元素ID
				if($selector['id']){
					$isid = $vv->getAttribute('id');
					if($isid!=$selector['id']){
						$hasit *= 0;	
					}
				}
				//判定元素是否有class
				if($selector['class']){
					$iscls = $vv->getAttribute('class');
					$iscls = explode(' ',$iscls);
					foreach($selector['class'] as $av){
						if(!in_array($av,$iscls)){
							$hasit *= 0;
						}
					}
				}
				//判定元素是否有该属性
				if($selector['attr']){
					foreach($selector['attr'] as $ak=>$av){
						if($vv->getAttribute($ak)!=$av){
							$hasit *= 0;	
						}
					}
				}
				if($hasit){
					$length++;
					$nodelist->push($vv);
				}
			}
		}
		$nodelist->length = $length;
		return $nodelist;
	}
}}